import 'package:flutter/material.dart';
import '../theme/app_theme.dart';
import '../utils/responsive_layout.dart';

/**
 * 现代化操作按钮尺寸枚举
 */
enum ModernCardSize {
  small,   // 小尺寸
  medium,  // 中等尺寸
  large,   // 大尺寸
}

/**
 * 现代化操作按钮组件
 * 用于快捷操作的按钮组件
 */
class ModernActionButton extends StatefulWidget {
  final IconData icon;
  final String? label;
  final VoidCallback? onTap;
  final ModernCardSize size;
  final String? tooltip;
  final Color? iconColor;
  final Color? backgroundColor;
  final bool isEnabled;

  const ModernActionButton({
    Key? key,
    required this.icon,
    this.label,
    this.onTap,
    this.size = ModernCardSize.medium,
    this.tooltip,
    this.iconColor,
    this.backgroundColor,
    this.isEnabled = true,
  }) : super(key: key);

  @override
  State<ModernActionButton> createState() => _ModernActionButtonState();
}

class _ModernActionButtonState extends State<ModernActionButton>
    with SingleTickerProviderStateMixin {
  bool _isHovered = false;
  bool _isPressed = false;
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 150),
      vsync: this,
    );
    
    _scaleAnimation = Tween<double>(
      begin: 1.0,
      end: 0.95,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    ));
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  void _handleHoverChange(bool isHovered) {
    if (!widget.isEnabled) return;
    
    setState(() {
      _isHovered = isHovered;
    });
    
    if (isHovered) {
      _animationController.forward();
    } else {
      _animationController.reverse();
    }
  }

  void _handleTapDown(TapDownDetails details) {
    if (!widget.isEnabled) return;
    
    setState(() {
      _isPressed = true;
    });
  }

  void _handleTapUp(TapUpDetails details) {
    if (!widget.isEnabled) return;
    
    setState(() {
      _isPressed = false;
    });
  }

  void _handleTapCancel() {
    if (!widget.isEnabled) return;
    
    setState(() {
      _isPressed = false;
    });
  }

  /**
   * 获取按钮尺寸
   */
  double _getButtonSize() {
    switch (widget.size) {
      case ModernCardSize.small:
        return 36;
      case ModernCardSize.medium:
        return 48;
      case ModernCardSize.large:
        return 56;
    }
  }

  /**
   * 获取图标尺寸
   */
  double _getIconSize() {
    switch (widget.size) {
      case ModernCardSize.small:
        return 18;
      case ModernCardSize.medium:
        return 24;
      case ModernCardSize.large:
        return 28;
    }
  }

  /**
   * 获取内边距
   */
  EdgeInsetsGeometry _getPadding() {
    if (widget.label != null) {
      switch (widget.size) {
        case ModernCardSize.small:
          return const EdgeInsets.symmetric(horizontal: 12, vertical: 8);
        case ModernCardSize.medium:
          return const EdgeInsets.symmetric(horizontal: 16, vertical: 12);
        case ModernCardSize.large:
          return const EdgeInsets.symmetric(horizontal: 20, vertical: 16);
      }
    } else {
      return EdgeInsets.all(_getButtonSize() / 4);
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;
    final isDesktop = ResponsiveBreakpoints.isDesktop(context);
    
    final backgroundColor = widget.backgroundColor ?? 
        (widget.isEnabled 
            ? colorScheme.surfaceVariant 
            : colorScheme.surfaceVariant.withOpacity(0.5));
    
    final iconColor = widget.iconColor ?? 
        (widget.isEnabled 
            ? colorScheme.onSurfaceVariant 
            : colorScheme.onSurfaceVariant.withOpacity(0.5));

    Widget buttonContent;
    
    if (widget.label != null) {
      // 带标签的按钮
      buttonContent = Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            widget.icon,
            size: _getIconSize(),
            color: iconColor,
          ),
          const SizedBox(width: 8),
          Text(
            widget.label!,
            style: theme.textTheme.bodyMedium?.copyWith(
              color: iconColor,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      );
    } else {
      // 只有图标的按钮
      buttonContent = Icon(
        widget.icon,
        size: _getIconSize(),
        color: iconColor,
      );
    }

    Widget button = AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return Transform.scale(
          scale: _isPressed ? 0.9 : _scaleAnimation.value,
          child: Container(
            height: widget.label != null ? null : _getButtonSize(),
            width: widget.label != null ? null : _getButtonSize(),
            decoration: BoxDecoration(
              color: _isHovered && widget.isEnabled
                  ? backgroundColor.withOpacity(0.8)
                  : backgroundColor,
              borderRadius: BorderRadius.circular(8),
              border: Border.all(
                color: colorScheme.outline.withOpacity(0.2),
                width: 1,
              ),
              boxShadow: _isHovered && widget.isEnabled
                  ? [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.1),
                        blurRadius: 4,
                        offset: const Offset(0, 2),
                      ),
                    ]
                  : null,
            ),
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                onTap: widget.isEnabled ? widget.onTap : null,
                onTapDown: _handleTapDown,
                onTapUp: _handleTapUp,
                onTapCancel: _handleTapCancel,
                onHover: _handleHoverChange,
                borderRadius: BorderRadius.circular(8),
                child: Padding(
                  padding: _getPadding(),
                  child: buttonContent,
                ),
              ),
            ),
          ),
        );
      },
    );

    // 如果有tooltip，包装Tooltip组件
    if (widget.tooltip != null) {
      button = Tooltip(
        message: widget.tooltip!,
        child: button,
      );
    }

    return button;
  }
}